package com.huanwei.cc.http;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.util.Map;

/**
 * Created by 智江鹏 on 2016/11/18.
 * 数据请求  Asynchtttp
 */

public class AsyncManaer {
    private Context context;

    private static AsyncManaer instance;

    public static AsyncManaer getInstance(Context context) {
        if (instance == null) {
            synchronized (AsyncManaer.class) {
                if (instance == null) {
                    instance = new AsyncManaer();
                }
            }
        }
        return instance;
    }

    private AsyncManaer() {
        this.context = context;

    }

    public void sendRequest(String url, Map<String, String> paramters, final Objectcallback callback) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(30000);
        RequestParams params = new RequestParams();
        if (paramters != null) {
            for (Map.Entry<String, String> me : paramters.entrySet()) {
                params.put(me.getKey(), me.getValue());

            }
        }

        client.post(context, url, params, new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  String responseString) {
                if (callback != null) {
                    callback.onsuccess(responseString);
                }

            }

            @Override
            public void onFailure(int arg0, Header[] arg1, String arg2,
                                  Throwable arg3) {
                if (callback == null) {
                    callback.onFalia(arg0, arg2);
                }

            }
        });


    }


    public interface Objectcallback {
        void onsuccess(String st);

        void onFalia(int code, String errst);

    }
    public void get(String url, Map<String, String> paramters,
                    final Objectcallback callback) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        if (paramters != null) {
            for (Map.Entry<String, String> me : paramters.entrySet()) {
                params.put(me.getKey(), me.getValue());

            }
        }

        client.get(context, url, params, new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  String responseString) {
                if (callback != null) {
                    callback.onsuccess(responseString);
                }

            }

            @Override
            public void onFailure(int arg0, Header[] arg1, String arg2,
                                  Throwable arg3) {
                if (callback == null) {
                    callback.onFalia(arg0, arg2);
                }

            }
        });}

}
